package com.ge.pp;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JPanel;

public class PicturePane extends JPanel {
	private PPImage myImage = null;
	private Patient myPatient = null;
	private Point curMousePos;
	
	public void paint(Graphics g) 
	{
		try
		{
			System.out.println(" paint ");
			super.paint(g);
			if(myImage != null && myPatient != null) 
			{	
				System.out.println(" image present ");
      		BufferedImage bi;
			if (filter != null) 
			{
				bi = filter.filter(myImage.bi);
			} 
			else 
			{
				bi = myImage.bi;
			}
			float [] pixelVal = new float[3];
			try
			{
				//////    Done fix for the 7:40pm    /////
				System.out.println(curMousePos.x+" "+curMousePos.y);
				int width  = bi.getWidth(this);
				int height = bi.getHeight(this);
				System.out.println(width+" "+height);
				if((curMousePos.x <width) &&(curMousePos.y < height))
				{
					bi.getData().getPixel(curMousePos.x, curMousePos.y, pixelVal);	
				}
				//////       OK    //////
			}
			catch(Exception e)
			{
				//System.out.println("cococ");
			}
			g.drawImage(bi, 0,20, null);
			
			String t =myPatient.lastname+", "+myPatient.firstname+" ("+Integer.toString(myPatient.id)+"):"+myImage.desc+" Val:"+pixelVal[0]+","+pixelVal[1]+","+pixelVal[2];
			g.drawChars(t.toCharArray(), 0, t.length(), 5, 15);
			
		} else {
			g.clearRect(0,0, this.getWidth(), getHeight());
		}
		}
		catch(Exception e)
		{
			try {
				throw e;
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}

	public PPViewer mainViewer;
	public ImageFilter filter;
	public Vector<ImageFilter> filterList;
	/*
	 * Ravi 
	 */
	public PicturePane(PPViewer _mainViewer) throws Exception 
	{
		try
		{
			System.out.println("PicturePane : in ");
		mainViewer = _mainViewer;
		initFilters();
		addMouseMotionListener(new MouseMotionListener() 
		{
			@Override
			public void mouseDragged(MouseEvent arg0) 
			{
				// TODO Auto-generated method stub				
			}

			@Override
			public void mouseMoved(MouseEvent evt) {
				curMousePos = evt.getPoint();
				repaint();
			}
		});
		}
		catch(Exception e)
		{
			throw e;
		}
	}
	
	private void initFilters() throws Exception 
	{
		try
		{filterList = new Vector<ImageFilter>();
		filterList.add(new WindowLevelFilter(this, 255, 128));
		filterList.add(new ThresholdFilter(this, 0));
		filterList.add(new SmoothingFilter(this));
		filterList.add(new SharpenFilter(this));
		}
		catch(Exception e)
		{
			throw e;
		}
	}
	public void setFilter(String name) throws Exception 
	{
		try
		{
			System.out.println("setFilter :"+name);
		filter = null;
		for(ImageFilter imgf:filterList) {
			if (imgf.name.equals(name)) {
				filter = imgf;
			}
		}
		}
		catch(Exception e)
		{
			throw e;
		}
	}
	/*
	 * Ravi
	 */
	public boolean ShowImage(PPImage img, Patient p) throws Exception 
	{				
		try
		{
			System.out.println("ShowInage : in ");
		myImage = img;
		myPatient = p;
		repaint();
		return true;			
		}
		catch(Exception e)
		{
			throw e;
		}
	}	
}